package com.guli.seckill.controller;

import com.guli.common.utils.R;
import com.guli.seckill.service.SeckillService;
import com.guli.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author zzb
 * @Date 2024/4/24 19:29
 * @Version 1.0
 */
@RestController
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 商品秒杀
     * @param killId
     * @param key
     * @param num
     * @param model
     * @return
     */
    @GetMapping("/kill")
    public R secKill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num,
                     Model model) {
        //判断是否登录
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return R.ok().put("data", orderSn);
    }

    /**
     * 查询秒杀商品的信息
     * @param skuId
     * @return
     */
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable Long skuId) {
        SeckillSkuRedisTo seckillSkuRedisTo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().put("data", seckillSkuRedisTo);
    }


    /**
     * 返回当前时间可以秒杀的商品
     * @return
     */
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> seckillSkuRedisTos = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data", seckillSkuRedisTos);
    }
}
